﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gmail.Model;
using Gmail.Presenters.Events;

namespace Gmail.Presenters
{
    public class EmailPresenter
    {
        IEmailView View;
        private Connector _Connector;
        private String t = "";

        private static EmailPresenter Singlton = null;
        public static EmailPresenter Instant(IEmailView View)
        {
            if (Singlton == null)
            {
                Singlton = new EmailPresenter(View);
            }
            return Singlton;
        }
        private EmailPresenter(IEmailView View)
        {
            this.View = View;
            this.View.EmailSubmitEvent += new EmailSubmitEventHandler(View_EmailSubmitEvent);
            this.View.ReloginEvent += new ReloginEventHandler(View_ReloginEvent);

        }

        void View_ReloginEvent(object sender, EventArgs e)
        {
            this.View.HideForm();
            App.ShowRelogin(_Filename, _DisplayFilename);
        }

        bool View_EmailSubmitEvent(object sender, EmailSubmitEventArgs e)
        {

            if (this._Connector != null)
            {
                return _Connector.Send(e.Email,_Filename,_DisplayFilename,e.Subject,e.Body);
            }
            throw new NotImplementedException();
        }

        private String _Filename;
        private String _DisplayFilename;
        public void ShowForm(String Filename, String DisplayFilename, Connector Connector)
        {
            _Filename = Filename;
            _DisplayFilename = DisplayFilename;
            this._Connector = Connector;
            t = Connector.Username;
            SortedDictionary<String, String> Emails = new SortedDictionary<string, string>();
            
            foreach (EmailEntry EmailEntry in Connector.EmailEntires)
            {
                Emails.Add(EmailEntry.Email, EmailEntry.ToString());
            }
            
            this.View.InitForm(Connector.Username, Emails,DisplayFilename);
        }

    }
}
